package service

import (
	"encoding/json"
	"web_framework/model"
	"web_framework/pkg/constvar"
	. "web_framework/pkg/e"
	"web_framework/storage"
)

//获取外接存储文件队列详情
func GetStoragePathQueueInfoCache(key string, sn string, pageNum int) ([]model.File, error) {
	start := (pageNum-1)*storage.RedisStoragePagePer
	end := pageNum*storage.RedisStoragePagePer - 1

	result, err := storage.RedisZRange(key, start, end)
	if err != nil {
		Log("Info", "获取Redis数据失败", err.Error())
		return nil, err
	}
	var PathListInfo []model.File
	for _, path := range result {
		storagePathInfoCacheKey := constvar.RouterRedisPrefix + sn  + storage.RedisStorageFileInfoKey + ":" + path
		res, err := storage.RedisGet(storagePathInfoCacheKey)
		if err != nil {
			Log("Info", "任务获取详情失败")
		}
		sliceFile := model.File{}
		json.Unmarshal([]byte(res), &sliceFile)
		PathListInfo = append(PathListInfo, sliceFile)
	}

	return PathListInfo, nil
}

// 获取对应的默认分区
func GetDefaultPath(sn string) (dPathKey string, err error) {
	partitionDPathCacheKey := constvar.RouterRedisPrefix + sn + storage.RedisStoragePartitionDPathKey
	dPathExists, err := storage.RedisHExists(partitionDPathCacheKey, "path")
	if err != nil || !dPathExists {
		Log("Info", "获取Redis数据失败")
		return "", err
	}
	dPathSn, _ := storage.RedisHGet(partitionDPathCacheKey, "sn")
	dPathUUID, _ := storage.RedisHGet(partitionDPathCacheKey, "uuid")
	dPathSeq, _ := storage.RedisHGet(partitionDPathCacheKey, "seq")
	dPathKey = Md5Str(dPathSn + dPathUUID + dPathSeq)
	return dPathKey, nil
}


